/*
 * Copyright 2016 drakeet. https://github.com/drakeet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.epfresh.adapter;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.epfresh.R;
import com.epfresh.activity.CommonStoreActivity;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.widget.tablayout.SlidingTabLayout;
import com.epfresh.api.widget.tablayout.listener.OnTabSelectListener;
import com.epfresh.bean.HomeCommTag;
import com.epfresh.bean.HomeGoodsOften;
import com.epfresh.bean.ListGoods;
import com.epfresh.views.tag.TagCloudView;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import multitype.ItemViewBinder;
import com.epfresh.api.utils.*;
public class ViewBinderHomeOftenGoods
        extends ItemViewBinder<HomeGoodsOften.ContentBean, ViewBinderHomeOftenGoods.ViewHolder> implements View.OnClickListener {

    OnCartAddListener onCartAddListener;
    HomeGoodsOften.ContentBean bean;
    public OnCartAddListener getOnCartAddListener() {
        return onCartAddListener;
    }

    public void setOnCartAddListener(OnCartAddListener onCartAddListener) {
        this.onCartAddListener = onCartAddListener;
    }

    public interface OnCartAddListener {
        public void onCartAdd(int type, int position, HomeGoodsOften.ContentBean goods, View v);
    }
    @Override
    public void onClick(View v) {
        Object o = v.getTag(R.id.item_key_position);

        if (o != null && o instanceof Integer) {
            int position = (int) o;
            if(v.getTag(R.id.item_key_status) instanceof HomeGoodsOften.ContentBean)
                bean = (HomeGoodsOften.ContentBean)v.getTag(R.id.item_key_status);
            else
                return;
/*            if ((v.getId() == R.id.iv_add)) {

                if (onAddClickListener != null) {
                    onAddClickListener.onItemClick(null, v, position, position);
                }

            } else */if (v.getId() == R.id.ed_price) {//购物车手动输入

                onCartAddListener.onCartAdd(1, position, bean, v);


            } else if (v.getId() == R.id.iv_add_count) {//添加
                    if (bean.isAcceptOrders())
                        onCartAddListener.onCartAdd(2, position, bean, v);
                    else
                        T.toast("暂不接单");

            } else if (v.getId() == R.id.iv_remove_count) {//减少
                    onCartAddListener.onCartAdd(3, position, bean, v);

            } else if (v.getId() == R.id.tv_product_title || v.getId() == R.id.iv_product || v.getId() == R.id.order_ll) {//
                    onCartAddListener.onCartAdd(5, position, bean, v);

            } else if (v.getId() == R.id.v_cart_bg) {

            } else if (v.getId() == R.id.rl_msg) {
                onCartAddListener.onCartAdd(10, position, bean, v);
            }else if(v.getId() == R.id.iv_often_delete){
                onCartAddListener.onCartAdd(0,position,bean,v);
            }else if(v.getId() == R.id.tv_home_often_compare){
                onCartAddListener.onCartAdd(7,position,bean,v);
            }

        }
    }


    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.include_home_often_goods, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder vh, @NonNull HomeGoodsOften.ContentBean bean) {
        initView(vh,bean);
    }

    void initView(@NonNull ViewHolder vh, @NonNull HomeGoodsOften.ContentBean bean) {
        vh.iv_often_delete.setTag(R.id.item_key_position,vh.getAdapterPosition());
        vh.iv_often_delete.setTag(R.id.item_key_status,bean);
        vh.iv_often_delete.setOnClickListener(this);

        vh.vCartBg.setTag(R.id.item_key_position, vh.getAdapterPosition());
        vh.vCartBg.setTag(R.id.item_key_status,bean);
        vh.vCartBg.setOnClickListener(this);
        vh.vContent.setTag(R.id.item_key_position, vh.getAdapterPosition());
        vh.vContent.setTag(R.id.item_key_status,bean);
        vh.vContent.setOnClickListener(this);
        vh.tvTitle.setTag(R.id.item_key_position, vh.getAdapterPosition());
        vh.tvTitle.setTag(R.id.item_key_status,bean);
        vh.tvTitle.setOnClickListener(this);
        if(bean.isShowOfferThree()){
            vh.tv_home_often_compare.setVisibility(View.GONE);
        }else {
            vh.tv_home_often_compare.setVisibility(View.GONE);
        }

        vh.tv_home_often_compare.setTag(R.id.item_key_position, vh.getAdapterPosition());
        vh.tv_home_often_compare.setTag(R.id.item_key_status,bean);
        vh.tv_home_often_compare.setOnClickListener(this);

        ImageLoader.getInstance().displayImage(bean.getThumbnail(), vh.iv);

        vh.tv_price.setText("" + bean.getPrice());
        //vh.tv_price_.setText("元/" + bean.getUnit());

/*
        vh.tvCountAdd.setText(goodsCount);
        vh.countAddUnderlineTv.setText(goodsCount);
        vh.tvChargeNumber.setText("已选" + goodsCount + p.getUnit());
*/

        String pack = bean.getPack();
        if (pack == null || "".equals(pack)) {
            vh.tvUnitPackage.setVisibility(View.INVISIBLE);
        } else {
            vh.tvUnitPackage.setVisibility(View.VISIBLE);
        }
        vh.tvUnitPackage.setText(pack);
        vh.tvCountAdd.setTag(R.id.item_key_position, vh.getAdapterPosition());
        vh.tvCountAdd.setTag(R.id.item_key_status,bean);
        vh.ivCountAdd.setTag(R.id.item_key_status,bean);
        vh.ivCountAdd.setTag(R.id.item_key_position, vh.getAdapterPosition());
        vh.ivCountRemove.setTag(R.id.item_key_position, vh.getAdapterPosition());
        vh.ivCountRemove.setTag(R.id.item_key_status,bean);
        vh.ivCountRemove.setTag(R.id.item_key_position, vh.getAdapterPosition());

        vh.tvCountAdd.setOnClickListener(this);
        vh.ivCountAdd.setOnClickListener(this);
        vh.ivCountRemove.setOnClickListener(this);


        vh.tvTitle.setText(bean.getTitle());
        vh.tv_home_often_store.setText(bean.getStoreName());

        String promotionType = bean.getPromotionItemType();

        if ("1".equals(promotionType)) {//1促销2秒杀
            vh.tv_activity_promotion.setVisibility(View.VISIBLE);
            vh.tv_activity_second.setVisibility(View.GONE);
        } else if ("2".equals(promotionType)) {//1促销2秒杀
            vh.tv_activity_promotion.setVisibility(View.GONE);
            vh.tv_activity_second.setVisibility(View.VISIBLE);
        } else {
            vh.tv_activity_promotion.setVisibility(View.GONE);
            vh.tv_activity_second.setVisibility(View.GONE);
        }
        String goodsCount = bean.getGoodsCount();


        vh.tvCountAdd.setText(goodsCount);
        vh.countAddUnderlineTv.setText(goodsCount);
        vh.tvChargeNumber.setText("已选" + goodsCount + bean.getChargeUnit());
        if ("".equals(goodsCount)) {
            showCartAddView(vh,false);
        } else {
            showCartAddView(vh,true);
        }
        ArrayList<HomeCommTag> tags = bean.getTags();
        if (tags != null && tags.size() > 0) {
            vh.tag.setVisibility(View.VISIBLE);
            vh.tag.setCommTags(tags);
        } else {
            vh.tag.setVisibility(View.GONE);
        }
        if (bean.isAcceptOrders()){
            vh.ivCountAdd.setImageResource(R.mipmap.price_add);
        }else{
            vh.ivCountAdd.setImageResource(R.mipmap.price_grey);
            showCartAddView(vh,false);
        }

        if (bean.isDisabled()){
            vh.tvTitle.setTextColor(Color.parseColor("#959595"));
            vh.tv_home_often_store.setTextColor(Color.parseColor("#959595"));
            vh.ll_home_often_no.setVisibility(View.VISIBLE);
            vh.ll_package.setVisibility(View.GONE);
        }else{
            vh.tvTitle.setTextColor(Color.parseColor("#313131"));
            vh.tv_home_often_store.setTextColor(Color.parseColor("#434343"));
            vh.ll_home_often_no.setVisibility(View.GONE);
            vh.ll_package.setVisibility(View.VISIBLE);
        }
    }

    public void showCartAddView(ViewHolder vh,boolean isShow) {
        if (isShow) {
            vh.ivCountAdd.setVisibility(View.VISIBLE);
            vh.ivCountRemove.setVisibility(View.VISIBLE);
            vh.tvCountAdd.setVisibility(View.VISIBLE);
            vh.countAddUnderlineLayout.setVisibility(View.VISIBLE);
            vh.tvChargeNumber.setVisibility(View.VISIBLE);

            vh.ivAdd.setVisibility(View.GONE);

        } else {
            vh.ivCountAdd.setVisibility(View.VISIBLE);
            vh.ivCountRemove.setVisibility(View.GONE);
            vh.tvCountAdd.setVisibility(View.GONE);
            vh.countAddUnderlineLayout.setVisibility(View.GONE);
            vh.tvChargeNumber.setVisibility(View.GONE);
            vh.vFlag.setVisibility(View.GONE);
            vh.ivAdd.setVisibility(View.GONE);
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        TextView tvUnitPackage;
        TextView tvMarketName;
        ImageView iv;
        ImageView ivAdd;
        ImageView ivCountAdd;
        ImageView ivCountRemove;
        TextView tvCountAdd;
        TextView tvChargeNumber;
        TextView countAddUnderlineTv;
        LinearLayout countAddUnderlineLayout;
        TextView tvPromotionStatus;
        ImageView ivCorner;
        ImageView ivCollect;
        View vCartBg;
        View vContent;
        View rlMsg;
        TextView tvMsg;
        View vFlag;
        View vArrow;

        View iv_often_delete;

        TextView tv_home_often_compare,tv_price,  tv_home_often_store, tv_activity_promotion, tv_activity_second;
        TagCloudView tag;

        View ll_home_often_no,ll_package;


        View rl_detail;

        ViewHolder(@NonNull View view) {
            super(view);
            iv = (ImageView) view.findViewById(R.id.iv_product);
            tv_home_often_compare = view.findViewById(R.id.tv_home_often_compare);
            tvTitle = (TextView) view.findViewById(R.id.tv_product_title);
            tvUnitPackage = (TextView) view.findViewById(R.id.tv_unit_package);
            tvChargeNumber = (TextView) view.findViewById(R.id.tv_charge_number);
            tv_price = (TextView) view.findViewById(R.id.tv_price_old);
            iv_often_delete = view.findViewById(R.id.iv_often_delete);

            tv_home_often_store = (TextView) view.findViewById(R.id.tv_home_often_store);

            ivCountAdd = (ImageView) view.findViewById(R.id.iv_add_count);
            ivCountRemove = (ImageView) view.findViewById(R.id.iv_remove_count);
            vCartBg = view.findViewById(R.id.v_cart_bg);
            vContent = view.findViewById(R.id.order_ll);


            ivAdd = (ImageView) view.findViewById(R.id.iv_add);
            tvCountAdd = (TextView) view.findViewById(R.id.ed_price);
            countAddUnderlineLayout = view.findViewById(R.id.item_center_search_underline_layout);
            countAddUnderlineTv = view.findViewById(R.id.item_center_search_ed_price_underline);

            ivCorner = (ImageView) view.findViewById(R.id.iv_corner);
            tag = (TagCloudView) view.findViewById(R.id.ll_tags);


            rlMsg = view.findViewById(R.id.rl_msg);
            tvMsg = (TextView) view.findViewById(R.id.tv_msg);
            vFlag = view.findViewById(R.id.iv_msg);
            vArrow = view.findViewById(R.id.tv_msg_right);
            ivCollect = view.findViewById(R.id.iv_collect);

            vCartBg = view.findViewById(R.id.v_cart_bg);

            tv_activity_promotion = view.findViewById(R.id.tv_activity_promotion);
            tv_activity_second = view.findViewById(R.id.tv_activity_second);
            tvPromotionStatus = (TextView) view.findViewById(R.id.tv_promotion_status);

            ll_home_often_no = view.findViewById(R.id.ll_home_often_no);
            ll_package = view.findViewById(R.id.ll_package);

            rl_detail = view.findViewById(R.id.rl_detail);
        }
    }
}
